package com.atguigu.gulimall.product.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author LiDeJie
 * @Create 2024/1/28 11:42
 * @Version 1.0
 */
//如果没注入到容器当中可以使用EnableConfigurationProperties()
//@EnableConfigurationProperties(value = {ThreadProperties.class})
@Component
public class ThreadConfig {
    
    
    @Bean
    public ThreadPoolExecutor threadPoolExecutor(ThreadProperties properties) {
        return new ThreadPoolExecutor(properties.getCoreSize(), properties.getMaxSize(), properties.getTimeOut(), 
                TimeUnit.SECONDS, 
                new ArrayBlockingQueue<>(properties.getQueueSize()),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
    }
    
}
